package com.jn.erp.web.api.student.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.common.core.page.TableDataInfo;
import com.jn.erp.utils.DateUtils;
import com.jn.erp.web.api.course.entity.vo.DataTrendsVo;
import com.jn.erp.web.api.course.mapper.TCourseOrderMapper;
import com.jn.erp.web.api.institution.entity.vo.EmployeeVo;
import com.jn.erp.web.api.student.entity.*;
import com.jn.erp.web.api.student.entity.dto.EnrollmentStuDto;
import com.jn.erp.web.api.student.entity.dto.StudentBaseInfoDTO;
import com.jn.erp.web.api.student.entity.dto.StudentRenewalDto;
import com.jn.erp.web.api.student.entity.vo.EnrollmentData;
import com.jn.erp.web.api.student.entity.vo.TStudentCourseVo;
import com.jn.erp.web.api.student.mapper.RenewalAlertsConfigMapper;
import com.jn.erp.web.api.student.mapper.StudentContactRecordMapper;
import com.jn.erp.web.api.student.mapper.StudentCourseMapper;
import com.jn.erp.web.api.student.mapper.StudentMapper;
import com.jn.erp.web.api.student.service.StudentClassService;
import com.jn.erp.web.api.student.service.StudentContactsUserService;
import com.jn.erp.web.api.student.service.StudentService;
import com.jn.erp.web.api.student.service.StudentUserService;
import com.jn.erp.web.api.student.utils.BizException;
import com.jn.erp.web.api.student.utils.UserAuthUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

import static com.jn.erp.security.utils.SecurityUtils.getUserId;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Resource
    private StudentClassService studentClassService;

    @Autowired
    private StudentCourseMapper studentCourseMapper;

    @Autowired
    private StudentUserService studentUserService;
    @Autowired
    private StudentContactsUserService studentContactsUserService;

    @Autowired
    private RenewalAlertsConfigMapper renewalAlertsConfigMapper;

    @Autowired
    private StudentContactRecordMapper studentContactRecordMapper;

    @Autowired
    private TCourseOrderMapper tCourseOrderMapper;

    @Autowired
    StudentCourseServiceImpl studentCourseServiceImpl;

    @Autowired
    private RenewalAlertsConfigMapper renewalAlertsMapper;

    @Override
    public List<Student> getList(Student student) {

        // 4是全部
        // 本周新增
        LocalDate localDate = LocalDate.now();
        if (student.getType().equals("1")) {
            LocalDate sTime = localDate.with(DayOfWeek.MONDAY);
            LocalDate eTime = localDate.with(DayOfWeek.SUNDAY);
            student.setSTime(sTime);
            student.setETime(eTime);
            //本月新增
        } else if (student.getType().equals("2")) {
            LocalDate sTime = localDate.with(TemporalAdjusters.firstDayOfMonth());
            LocalDate eTime = localDate.with(TemporalAdjusters.lastDayOfMonth());
            student.setSTime(sTime);
            student.setETime(eTime);
            // 今日新增
        } else if (student.getType().equals("3")) {
            student.setSTime(localDate);
            student.setETime(localDate);
            // 回收站 delete = 1
        } else if (student.getType().equals("5")) {
            student.setDeleted(1);
        } else if (student.getStage() != null && student.getStage().equals(1) && student.getMine().equals("1")) {
            student.setCreator(getUserId());
        }
        List<Student> rows = studentMapper.getList(student);
        // 如果是历史学员 加上这个学员每个课的剩余课时
        for (Student row : rows) {
            if (row.getStage() == 5) {
                // 这个学生的所有订单
                List<StudentCourse> studentCourse = studentCourseMapper.selectOrder(row.getId());
                row.setStudentCourseList(studentCourse);
            }
        }
        return rows;
    }

    @Override
    public boolean delete(List<Long> ids) {
        if (this.removeByIds(ids)) {
            studentClassService.deleteClassStudent(ids);
            return true;
        }
        return false;
    }

    @Override
    public String selCount(EmployeeVo employeeVo) {
        return studentMapper.selNum(employeeVo);
    }

    @Override
    public List<EnrollmentStuDto> enrollmentList(EnrollmentStuDto student) {

        List<EnrollmentStuDto> list = studentMapper.enrollmentList(student);

        //如果结课了
        BigDecimal exhaustMoney = new BigDecimal(0);
        for (EnrollmentStuDto dto : list) {
            // 课消金额
            BigDecimal num = new BigDecimal(0);
            BigDecimal i = studentCourseMapper.selAmountTotal(dto.getStudentId());
            // 查询一个学生的所有报读课程
            QueryWrapper<StudentCourse> qw = new QueryWrapper<>();
            qw.eq("org_id", student.getOrgId());
            qw.eq("student_id", dto.getId());
            List<StudentCourse> studentCourses = studentCourseMapper.selectList(qw);

            // 剩余金额
            BigDecimal remainderAmount = new BigDecimal(0);
            BigDecimal spendAmount = new BigDecimal(0);
            for (StudentCourse studentCours : studentCourses) {
                // 如果没结课
                if (studentCours.getExhausted() == 1) {
                    if (dto.getStudentId() == studentCours.getStudentId()) {
                        // 课消金额  实付金额/购买总课次(总)*实扣课时（总）
                        // 购买课时为0
                       if (studentCours.getCountLessonTotalNormal() == 0){
                            spendAmount = new BigDecimal(0);
                       } else {
                            spendAmount = studentCours.getPaidAmount().divide(new BigDecimal(studentCours.getCountLessonTotalNormal()), BigDecimal.ROUND_CEILING).multiply(new BigDecimal(studentCours.getCountLessonComplete()));
                       }
                        num = num.add(spendAmount);
                    }
                    // 如果超上
                    // 剩余课时 总-消耗-退课 如果结课剩余数量变0
                    int overPowered = 0;
                   if (studentCours.getCountLessonTotal() < studentCours.getCountLessonComplete()){
                       overPowered = studentCours.getCountLessonTotal() - studentCours.getCountLessonComplete();
                    }
                }

                // 如果结课且有消课 取 全部报名金额 X

                if (studentCours.getExhausted() == 0 ) {
                    // 如果结课且 退课课时=全部课时 消课金额为0
                    if (studentCours.getCountLessonRefund() == studentCours.getCountLessonTotal()){
                        exhaustMoney = new BigDecimal(0);

                    } else if (studentCours.getCountLessonComplete() < studentCours.getCountLessonTotalNormal()){
                        if (studentCours.getCountLessonTotalNormal() == 0){
                            exhaustMoney = new BigDecimal(0);
                        } else {
                            exhaustMoney = studentCours.getPaidAmount().divide(new BigDecimal(studentCours.getCountLessonTotalNormal()), BigDecimal.ROUND_CEILING).multiply(new BigDecimal(studentCours.getCountLessonComplete()));
                        }
                    } else {
                        // 课消金额等于报名金额
                        // 结课的金额
                        exhaustMoney = studentCourseMapper.selexhaustMoney(dto);
                    }
                }
            }
            // 剩余金额
            num = num.add(exhaustMoney);
            dto.setSpendAmount(num);
            // 剩余课消金额 = 实付 - 课消金额 - 退的(结课的)
            remainderAmount = i.subtract(dto.getSpendAmount()).subtract(dto.getRefundAmount());
            dto.setRemainderAmount(remainderAmount);
        }
        return list;
    }


    @Override
    public boolean changeStage(Long id, Integer stage, boolean force) {
        if (stage == 5) {
            Student stu = this.getById(id);
            stu.setChangeStageTime(new Date());
            //查学员总剩余多少课时
//        BigDecimal remaining = studentCourseService.getTotalLessonRemainingCount(stu.getId());
//        if (5 == stage && remaining != null) {
//            if (force) {
//                throw new BizException(stu.getName() + "的课次有剩余，无法结业");
//            }
//        }
            //        if (5 == stage) {
//            stu.setGraduationDate(LocalDate.now());
//        }
            return this.updateById(stu);

        } else {
            // 转为线索
            UpdateWrapper<Student> up = new UpdateWrapper<>();
            up.eq("id", id);
            up.set("stage", stage);
            up.set("counselor_id", null);
            up.set("management_id", null);
            return this.update(up);
        }

    }

    @Override
    public Long saveOrUpdateByDTO(StudentBaseInfoDTO dto) {
        if (StringUtils.isBlank(dto.getName())) {
            throw new BizException("学生姓名须提供一个");
        }

        StudentUser user = studentUserService.existOrCreate(dto.getMobile(), dto.getSchoolId());
        // id == null 创建新学员
        Long userId = UserAuthUtil.getCurrentUserId();
        if (dto.getCounselorId() == null) {
            dto.setCounselorId(userId);
        }
        Student student = new Student();
        BeanUtils.copyProperties(dto, student);
        student.setUserId(user.getId());
        student.setCreator(dto.getCreator());
        // 学生来源
        student.setJoinWay(dto.getJoinWay());
        if (dto.getId() == null) {
            student.setAddTime(LocalDateTime.now());
            student.setStage(1);
        } else {
            student.setStage(dto.getStage());
            student.setEditTime(LocalDateTime.now());
        }
        this.saveOrUpdate(student);
        user.setFamilyRel(student.getFamilyRel());
        studentUserService.updateById(user);
        return student.getId();
    }

    public List<Student> listByUid(Long userId) {
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        return list(qw);

    }


    @Override
    public Student getStudent(Student student) {
        Student reStudent = studentMapper.getStudent(student);
        //获取默认
        StudentUser de = new StudentUser();
        de.setFamilyRel(reStudent.getFamilyRel());
        de.setMobile(reStudent.getMobile());
        reStudent.setDefaultUser(de);
        //获取备选
        QueryWrapper<StudentContactsUser> qw = new QueryWrapper<>();
        qw.eq("is_backup", 1);
        qw.eq("student_id", reStudent.getId());
        StudentContactsUser one = studentContactsUserService.getOne(qw);
        if (one != null) {
            StudentUser bu = new StudentUser();
            bu.setFamilyRel(one.getFamilyRel());
            QueryWrapper<StudentUser> qwu = new QueryWrapper<>();
            qwu.eq("id", one.getUserId());
            StudentUser one1 = studentUserService.getOne(qwu);
            if (one1 != null) {
                bu.setMobile(one1.getMobile());
            }
            reStudent.setBackupUser(bu);
        }
        return reStudent;
    }

    @Override
    public Student applicationOne(Student student) {
        Student stu = studentMapper.applicationOne(student);
        String courseName = stu.getCourseName();
        if (StringUtils.isEmpty(stu.getCourseName())) {
            stu.setCourses(null);
        } else {
            String[] strArray = courseName.split(",");
            stu.setCourses(strArray);
        }
        stu.setSpendAmount(stu.getAmountSpent());
        return stu;
    }


    @Override
    public Student deleteClue(Student student) {
        Student stu = studentMapper.selectById(student.getId());
        // 放入回收站
        if (student.getType().equals("1")) {
            stu.setDeleted(1);
            stu.setDeletePeopleId(getUserId());
            stu.setDeleteReason(student.getDeleteReason());
            stu.setEditTime(LocalDateTime.now());
            stu.setDeleteTime(LocalDateTime.now());
            studentMapper.updateById(stu);

            // 彻底删除
        } else if (student.getType().equals("2")) {
            if (stu.getStage() == 1 && stu.getDeleted() == 1) {
                studentMapper.deleteById(stu);
            }
            // 复原
        } else if (student.getType().equals("3")) {
            if (stu.getDeleted() == 1) {
                // 如果是跟进学员 把跟进记录的数据删掉
                if (stu.getStage() == 3) {
                    if (StrUtil.isNotBlank(student.getRecordId())) {
                        StudentContactRecord studentContactRecord = studentContactRecordMapper.selOne(student.getRecordId());
                        if (studentContactRecord != null) {
                            studentContactRecord.setDeleted(0);
                            studentContactRecordMapper.updateById(studentContactRecord);
                        }
                    }
                }
                stu.setDeleted(0);
                stu.setEditTime(LocalDateTime.now());
                studentMapper.updateById(stu);
            }
        }
        return null;
    }


    @Override
    public void getCounselor(Student student) {
        Long orgId = 0l;
        boolean saveRecord = true;
        if (student.getManagementId() != null) {
            saveRecord = false;
        }
        List<Long> ids = student.getIds();
        for (Long id : ids) {
            Student stu = studentMapper.selectById(id);
            orgId = stu.getOrgId();
//            1, 意向学员 2, 在学学员 3 跟进学员 4.报读学员 5.历史学员
            if (stu.getStage() == 1) {
                // 分配跟进人
                stu.setCounselorId(student.getCounselorId());
                stu.setStage(3);
                //分配学管师
                stu.setManagementId(student.getManagementId());
                stu.setType("1");
                studentMapper.updateById(stu);

            } else {
                // 除了线索学员分配跟进人变成跟进管理， 其他的学员状态不变
                // 分配跟进人
                stu.setCounselorId(student.getCounselorId());
                //分配学管师
                stu.setManagementId(student.getManagementId());
                stu.setType("2");
                studentMapper.updateById(stu);
            }
            if (saveRecord) {
                // 添加一条跟进记录
                StudentContactRecord studentContactRecord = new StudentContactRecord();
                studentContactRecord.setCreator(getUserId());
                //  默认跟进时间
                studentContactRecord.setAddTime(LocalDateTime.now());
                studentContactRecord.setStudentId(stu.getId());

                //分配跟进人 在读报读中记录添加默认，跟进阶段-》更新跟进人）
                //跟进时间(默认操作时间)
                studentContactRecord.setContactTime(LocalDate.now());
                //跟进方式(其他)
                studentContactRecord.setState("5");
                //跟进阶段(更新跟进人)
                studentContactRecord.setPhase("10");
                studentContactRecord.setOrgId(orgId);
                studentContactRecordMapper.insert(studentContactRecord);
            }

        }
    }


    //////////////////////////统一新建线索/////////////////////////////////////////////
    @Override
    public Long studentUnifysaveOrUpdateByDTO(StudentBaseInfoDTO dto) {
        Student student = new Student();
        try {

            StudentUser defaultUser = dto.getDefaultUser();
            if (StringUtils.isBlank(dto.getName()) && defaultUser != null && StrUtil.isNotBlank(defaultUser.getMobile())) {
                throw new BizException("学生姓名须提供一个，且有关联手机号");
            }
            defaultUser.setOrgId(dto.getOrgId());
            defaultUser = studentUserService.existOrCreate(defaultUser);
            StudentUser backupUser = dto.getBackupUser();
            if (backupUser != null && StrUtil.isNotBlank(backupUser.getMobile())) {
                backupUser.setOrgId(dto.getOrgId());
                backupUser = studentUserService.existOrCreate(backupUser);

            }
// id == null 创建新学员
            Long userId = UserAuthUtil.getCurrentUserId();
            if (dto.getCounselorId() == null) {
                dto.setCounselorId(userId);
            }
            // 查询cof_id
            QueryWrapper<TRenewalAlertsConfig> alertQw = new QueryWrapper<>();
            alertQw.eq("org_Id", dto.getOrgId());
            alertQw.eq("is_delete", 0);
            TRenewalAlertsConfig renewalAlerts = renewalAlertsConfigMapper.selectOne(alertQw);

            BeanUtils.copyProperties(dto, student);
            student.setUserId(defaultUser.getId());
            student.setCreator(dto.getCreator());
            // 学生来源
            student.setJoinWay(dto.getJoinWay());
            student.setAddTime(LocalDateTime.now());
            student.setFamilyRel(defaultUser.getFamilyRel());
            student.setCofId(Math.toIntExact(renewalAlerts.getId()));
//            // 新加的线索学员 配置是1
//            student.setCofId(1);
            if (dto.getId() == null) {
                student.setStage(1);
            } else {
                student.setStage(dto.getStage());
            }

            //新建或修改时姓名和主手机号唯一性判断
            QueryWrapper<Student> qws = new QueryWrapper<>();
            qws.eq("user_id", student.getUserId());
            qws.eq("name", student.getName());

            qws.eq("org_id", dto.getOrgId());
            if (student.getId() != null) {
                qws.ne("id", student.getId());
            }
            List<Student> list = this.list(qws);
            if (list.size() > 0) {
                throw new RuntimeException("机构下存在相同数据");
            }

            this.saveOrUpdate(student);

            if (dto.getId() != null) {
                QueryWrapper<StudentContactsUser> qw = new QueryWrapper();
                qw.eq("student_id", dto.getId());
                studentContactsUserService.remove(qw);
            }
            List<StudentContactsUser> clist = new ArrayList<>();
            StudentContactsUser c = new StudentContactsUser();
            c.setStudentId(student.getId());
            c.setUserId(defaultUser.getId());
            c.setFamilyRel(defaultUser.getFamilyRel());
            c.setIsBackup(0);
            clist.add(c);
            if (backupUser != null) {
                StudentContactsUser cb = new StudentContactsUser();
                cb.setStudentId(student.getId());
                cb.setUserId(backupUser.getId());
                cb.setFamilyRel(backupUser.getFamilyRel());
                cb.setIsBackup(1);
                clist.add(cb);
            }
            studentContactsUserService.saveBatch(clist);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return student.getId();
    }


    @Override
    public TableDataInfo enrollmentListPage(EnrollmentStuDto student) {
        return null;
    }

    @Override
    public EnrollmentStuDto remainderCount(EnrollmentStuDto student) {
        EnrollmentStuDto enrollmentStuDto = new EnrollmentStuDto();
        // 如果没有结课
        student.setExhausted(1);
        EnrollmentStuDto e = tCourseOrderMapper.countMoney(student);
        // 剩余总课时
        int remainderLessonTotal = e.getCountLessonTotal() - e.getCountLessonComplete() - e.getCountLessonRefund();
        enrollmentStuDto.setRemainderLessonTotal(remainderLessonTotal);
        // 课消金额
        enrollmentStuDto.setRemainderAmountTotal(e.getDiscountAmount());
        return enrollmentStuDto;

    }

    @Override
    public List<Student> allStudent(Student student) {
        return studentMapper.allStudent(student);
    }

    @Override
    public List<Student> studentList(Student student) {
        return studentMapper.studentList(student);
    }

    @Override
    public String changeStageHisyory(Student student) {
        Student byId = studentMapper.selectById(student.getId());
        // 转为历史 添加历史原因和时间
        Student stu = this.getById(student.getId());
        //查学员总剩余多少课时
//        int remaining = studentCourseService.getTotalLessonRemainingCount(stu.getId());
//            if (remaining>0) {
//                throw new ServiceException("课次有剩余，无法转为历史");
//            }
        // 清空这个学员的数据
        TStudentCourseVo tStudentCourseVo = new TStudentCourseVo();
        tStudentCourseVo.setStudentId(student.getId());
        tStudentCourseVo.setEditor(getUserId());
        tStudentCourseVo.setOrgId(byId.getOrgId());
        studentCourseServiceImpl.dealClassEnding(tStudentCourseVo, 3);
        byId.setTurnHistoryDate(LocalDate.now());
        byId.setTurnHistoryReason(student.getTurnHistoryReason());
        byId.setStage(5);
        studentMapper.updateById(byId);
        return "成功";
    }

    @Override
    public String selManageCount(EmployeeVo employeeVo) {
        return studentMapper.selManageCount(employeeVo);
    }

    @Override
    public List<StudentRenewalDto> alertsList(StudentRenewalDto studentRenewalDto) {

        studentRenewalDto.setType(studentRenewalDto.getType());
        List<StudentRenewalDto> stu = studentMapper.alertsList(studentRenewalDto);
        // 学生的全部课时
        return stu;
    }


    @Override
    public Map clueDataNum(DataTrendsVo dataTrendsVo) {
        Map result = new HashMap();
        String lastDay = "";
        String lastWeek = "";
        String lastStartDay = "";
        String lastStartWeek = "";

        if (2 == dataTrendsVo.getType()) {
            dataTrendsVo.setStartDate(dataTrendsVo.getStartDate() + "-01");
            dataTrendsVo.setEndDate(dataTrendsVo.getEndDate() + "-31");
//            try {
//                dataTrendsVo.setEndDate(DateUtils.getEndDayByMonth(dataTrendsVo.getEndDate()));
//            } catch (ParseException e) {
//                throw new RuntimeException(e);
//            }
        }

        try {
            // 按日
            if (1 == dataTrendsVo.getType()) {
                // 上周的当天 、昨天
                lastDay = DateUtils.getLastDay(dataTrendsVo.getEndDate(), -1);
                lastWeek = DateUtils.getLastDay(dataTrendsVo.getEndDate(), -7);
                lastStartDay = DateUtils.getLastDay(dataTrendsVo.getStartDate(), -1);
                lastStartWeek = DateUtils.getLastDay(dataTrendsVo.getStartDate(), -7);
            } else if (2 == dataTrendsVo.getType()) {
                // 去年的当月/上月
                lastDay = DateUtils.getEndDayByMonth(DateUtils.getLastMonth(dataTrendsVo.getEndDate(), 1));
                lastWeek = DateUtils.getLastYear(dataTrendsVo.getEndDate(), -1);
                lastStartDay = DateUtils.getLastMonth(dataTrendsVo.getStartDate(), 1) + "-01";
                lastStartWeek = DateUtils.getLastYear(dataTrendsVo.getStartDate(), -1);
            }
            // 当天的数据
            // 线索数据
            EnrollmentData enrollmentData = studentMapper.clueDataNum(dataTrendsVo);
            // 试听数据
            EnrollmentData auditionData = studentMapper.auditionData(dataTrendsVo);
            // 报名/ 金额数据
            EnrollmentData paidAmount = studentMapper.selPaid(dataTrendsVo);

            // 昨天的数据
            dataTrendsVo.setStartDate(lastDay);
            dataTrendsVo.setEndDate(lastStartDay);
            EnrollmentData day = studentMapper.clueDataNum(dataTrendsVo);
            EnrollmentData auditionDataDay = studentMapper.auditionData(dataTrendsVo);
            // 报名/ 金额数据
            EnrollmentData paidAmountDay = studentMapper.selPaid(dataTrendsVo);
            // 上周的
            dataTrendsVo.setStartDate(lastWeek);
            dataTrendsVo.setEndDate(lastStartWeek);
            EnrollmentData week = studentMapper.clueDataNum(dataTrendsVo);
            EnrollmentData auditionDataWeek = studentMapper.auditionData(dataTrendsVo);
            // 报名/ 金额数据
            EnrollmentData paidAmountWeek = studentMapper.selPaid(dataTrendsVo);
            // 全部线索数
            result.put("totalNum", enrollmentData.getTotalNum());
            result.put("totalNumDay", day.getTotalNum());
            result.put("totalNumWeek", week.getTotalNum());
            // 新增线索
            result.put("newNum", enrollmentData.getNewNum());
            result.put("newNumDay", day.getNewNum());
            result.put("newNumWeek", week.getNewNum());
            // 预约试听
            result.put("bookNum", auditionData.getBookNum());
            result.put("bookNumDay", auditionDataDay.getBookNum());
            result.put("bookNumWeek", auditionDataWeek.getBookNum());
            // 试听完成
            result.put("finishNum", auditionData.getFinishNum());
            result.put("finishNumDay", auditionDataDay.getFinishNum());
            result.put("finishNumWeek", auditionDataWeek.getFinishNum());
            // 试听报名
            result.put("applicationNum", auditionData.getApplicationNum());
            result.put("applicationNumDay", auditionDataDay.getApplicationNum());
            result.put("applicationNumWeek", auditionDataWeek.getApplicationNum());
            // 试听完成率
            result.put("completionRate", auditionData.getCompletionRate());
            result.put("completionRateDay", auditionDataDay.getCompletionRate());
            result.put("completionRateWeek", auditionDataWeek.getCompletionRate());
            // 试听转化率
            result.put("conversionRate", auditionData.getConversionRate());
            result.put("conversionRateDay", auditionDataDay.getConversionRate());
            result.put("conversionRateWeek", auditionDataWeek.getConversionRate());
            // 报课人次
            result.put("registraNum", paidAmount.getRegistraNum());
            result.put("registraNumDay", paidAmountDay.getRegistraNum());
            result.put("registraNumWeek", paidAmountWeek.getRegistraNum());
            // 实付金额
            result.put("paidAmount", paidAmount.getPaidAmount());
            result.put("paidAmountDay", paidAmountDay.getPaidAmount());
            result.put("paidAmountWeek", paidAmountWeek.getPaidAmount());


        } catch (ParseException e) {
            e.printStackTrace();
        }

        return result;

    }

    @Override
    public List<EnrollmentData> rankingList(DataTrendsVo dataTrendsVo) {

        if (dataTrendsVo.getType() == 1) {
            // 学员创建人排行
            if (dataTrendsVo.getCode() == 2) {
                List<EnrollmentData> enrollmentData = studentMapper.rankingList(dataTrendsVo);
                return enrollmentData;
            }
            // 上课老师排行
            if (dataTrendsVo.getCode() == 3) {
                List<EnrollmentData> TeacherList = studentMapper.TeacherList(dataTrendsVo);
                return TeacherList;
            }
            // 日期趋势排行
            if (dataTrendsVo.getCode() == 5) {
                List<EnrollmentData> payData = studentMapper.payData(dataTrendsVo);
                return payData;
            }

        } else if (dataTrendsVo.getType() == 2) {
            if (dataTrendsVo.getCode() == 2) {
                List<EnrollmentData> enrollmentData = studentMapper.rankingMonthList(dataTrendsVo);
                return enrollmentData;
            }
            if (dataTrendsVo.getCode() == 3) {
                List<EnrollmentData> TeacherList = studentMapper.TeacherMonthList(dataTrendsVo);
                return TeacherList;
            }
            if (dataTrendsVo.getCode() == 5) {
                List<EnrollmentData> payData = studentMapper.payDataMonth(dataTrendsVo);
                return payData;
            }
        }

        return null;
    }


    @Override
    public Map<String, List<List>> dataTrendsEChart(DataTrendsVo dataTrendsVo) {
        List<EnrollmentData> clueNum = null;
        List<EnrollmentData> auditionNum = null;
        List<EnrollmentData> paidAmount = null;
        if (dataTrendsVo.getType() == 1) {
            // 新增线索数
            clueNum = studentMapper.payData(dataTrendsVo);
//            // 试听数
//            auditionNum = studentMapper.auditionTrendsEChartDay(dataTrendsVo);
//            // 金额日
//             paidAmount = studentMapper.paidAmountListDay(dataTrendsVo);


        } else if (dataTrendsVo.getType() == 2) {
            // 新增线索数
//            clueNum = studentMapper.clueTrendsEChartMonth(dataTrendsVo);
            clueNum = studentMapper.payDataMonth(dataTrendsVo);
            // 试听数
//            auditionNum = studentMapper.auditionTrendsEChartMonth(dataTrendsVo);
//            // 金额日
//            paidAmount = studentMapper.paidAmountListMonth(dataTrendsVo);

        }

        Map<String, List<List>> result = new HashMap<>();
        List<List> totalNumList = new ArrayList<>();
        List<List> newNumList = new ArrayList<>();
        List<List> bookNumList = new ArrayList<>();
        List<List> finishNumList = new ArrayList<>();
        List<List> applicationNumList = new ArrayList<>();
        List<List> completionRateList = new ArrayList<>();
        List<List> conversionRateList = new ArrayList<>();
        List<List> registraNumList = new ArrayList<>();
        List<List> paidAmountNumList = new ArrayList<>();

        // 试听数
        for (EnrollmentData list : clueNum) {
            String date = list.getAddTime();
            List newNum = new ArrayList<>();
            List paidAmountNum = new ArrayList<>();
            List registraNum = new ArrayList<>();
            List bookNum = new ArrayList<>();
            List finishNum = new ArrayList<>();
            List applicationNum = new ArrayList<>();
            List completionRate = new ArrayList<>();
            List conversionRate = new ArrayList<>();

            //预约试听数
            bookNum.add(date);
            bookNum.add(list.getBookNum());
            bookNumList.add(bookNum);
            // 试听完成数
            finishNum.add(date);
            finishNum.add(list.getFinishNum());
            finishNumList.add(finishNum);
            // 试听报名数
            applicationNum.add(date);
            applicationNum.add(list.getApplicationNum());
            applicationNumList.add(applicationNum);
            // 试听完成率
            completionRate.add(date);
            completionRate.add(list.getCompletionRate());
            completionRateList.add(completionRate);
            // 试听转化率
            conversionRate.add(date);
            conversionRate.add(list.getConversionRate());
            conversionRateList.add(conversionRate);

            // 新增线索数
            newNum.add(date);
            newNum.add(list.getNewNum());
            newNumList.add(newNum);


            // 缴费金额
            paidAmountNum.add(date);
            paidAmountNum.add(list.getPaidAmount());
            paidAmountNumList.add(paidAmountNum);
            // 报课人次
            registraNum.add(date);
            registraNum.add(list.getRegistraNum());
            registraNumList.add(registraNum);
        }


        result.put("newNumList", newNumList);
        result.put("totalNumList", totalNumList);
        result.put("bookNumList", bookNumList);
        result.put("finishNumList", finishNumList);
        result.put("applicationNumList", applicationNumList);
        result.put("completionRateList", completionRateList);
        result.put("conversionRateList", conversionRateList);
        result.put("registraNumList", registraNumList);
        result.put("paidAmountNumList", paidAmountNumList);

        return result;
    }

    @Override
    public List<Student> listSource() {
        return studentMapper.listSource();
    }

    @Override
    public Long getUserIdAlertsCount(StudentRenewalDto studentRenewalDto) {
        return baseMapper.getUserIdAlertsCount(studentRenewalDto);
    }

}
